var {
  get,
  post
} = require('./../../comm/localHttp');
Page({
  data: {
    examId: '',
    currentQuestionIndex: 0,
    totalQuestions: 0,
    currentQuestion: {},
    answeredQuestions: [],
    examData: {},
    currentQt: {},
    process: 0,
    processInfo: '',
  },

  onLoad: function (e) {
    console.log(e);
    var examId = e.examId;
    this.setData({
      examId: examId
    });
    this.loadExamData(examId);
  },

  setProcessInfo() {
    if (this.data.examData == null || this.data.examData.listHistoryQuestion == null || this.data.examData.listHistoryQuestion.length == 0) {
      this.setData({
        process: 0,
        processInfo: '无题目'
      });
    } else {
      this.setData({
        process: (this.data.currentQuestionIndex + 1) * 100 / this.data.examData.listHistoryQuestion.length,
        processInfo: (this.data.currentQuestionIndex + 1) + '/' + this.data.examData.listHistoryQuestion.length,
      });
    }
  },
  loadExamData: async function (examId) {
    var examData = {};
    var currentQt = {};
    var currentQuestionIndex = -1;
    const res = await get('/exam/detail', {
      id: examId
    });

    if (res.code == 0) {
      console.log('exam data:', res.data);
      examData = res.data;
      if (examData.listHistoryQuestion.length > 0) {
        currentQuestionIndex = 0;
        currentQt = examData.listHistoryQuestion[0];
      }
      examData.listHistoryQuestion.forEach(element => {
        element.listItemInfo.forEach(item => {
          item.checked = false;
        })
      });

    }
    this.setData({
      examData: examData,
      currentQt: currentQt,
      currentQuestionIndex: currentQuestionIndex
    });
    this.setProcessInfo();
    console.log(currentQt);
  },

  onOptionChange: function (e) {

    console.log('radio发生change事件，携带value值为：', e)
    const currentQt = this.data.currentQt;
    for (let i = 0, len = currentQt.listItemInfo.length; i < len; ++i) {
      currentQt.listItemInfo[i].checked = (i == e.currentTarget.dataset.index);
      console.log('checked', currentQt.listItemInfo[i].checked)
    }
    this.setData({
      currentQt
    })
  },

  onOptionMutilChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e)
    const currentQt = this.data.currentQt;
    const index = e.currentTarget.dataset.index;
    currentQt.listItemInfo[index].checked = !currentQt.listItemInfo[index].checked;
    this.setData({
      currentQt
    })
  },

  btn_click_pre: function () {
    if (this.data.currentQuestionIndex > 0) {
      if (this.data.examData != null && this.data.examData.listHistoryQuestion != null &&
        this.data.currentQuestionIndex - 1 < this.data.examData.listHistoryQuestion.length) {
        this.setData({
          currentQt: this.data.examData.listHistoryQuestion[this.data.currentQuestionIndex - 1],
          currentQuestionIndex: this.data.currentQuestionIndex - 1,
        });
      }
    }
    this.setProcessInfo();
  },

  btn_click_next: function () {
    if (this.data.currentQuestionIndex < this.data.examData.listHistoryQuestion.length - 1) {
      this.setData({
        currentQt: this.data.examData.listHistoryQuestion[this.data.currentQuestionIndex + 1],
        currentQuestionIndex: this.data.currentQuestionIndex + 1,
      });
    }
    this.setProcessInfo();
  },
  btn_click_submit: async function () {
    const userInfo = wx.getStorageSync('userInfo');

    const examUserInfo = {
      answerStatus: 1,
      userId: userInfo.userId,
      examId: this.data.examId,
      officeCode: userInfo.officeCode,
      listUserAnswer: [],
    };
    let score = 0;
    for (let i = 0, len = this.data.examData.listHistoryQuestion.length; i < len; i++) {
      console.log(i);
      var qt = this.data.examData.listHistoryQuestion[i];
      var hasSelect = false;
      for (let j = 0, lenJ = qt.listItemInfo.length; j < lenJ; j++) {
        console.log(j);
        const option = qt.listItemInfo[j];
        if (option.checked) {
          hasSelect = true;
          examUserInfo.listUserAnswer.push({
            userId: userInfo.userId,
            examId: this.data.examId,
            hqbId: option.hqbId,
            answerId: option.qbItemId,
            score: option.score
          });
          score += option.score;
        }
      }
      examUserInfo.score = score;
      if (hasSelect == false) {
        wx.showModal({
          title: '还有内容未完成',
          content: '第 ' + (i + 1) + ' 未答题!'           
        })
        return;
      }
    }
    console.log('examUserInfo', examUserInfo);
    wx.showModal({
      title: '提示',
      content: '点击提交问卷',
      success: async (res) => {
        if (res.confirm) {
          const res = await post('/publish/answer', examUserInfo);
          if (res.code == 0) {
            wx.showModal({
              title: '提示!',
              content: '已提交'
            });
            wx.navigateTo({
                url: '/pages/index/index?viewType=1'
            });
          } else {
            wx.showModal({
              title: '错误',
              content: 'code:' + res.code + ';msg:' + res.msg
            });
          }
        }
      }
    })
  }
});